package com.ruoyi.app.controller.common.service.impl;

import com.ruoyi.app.controller.marketing.mapper.ICouponMapper;
import com.ruoyi.app.controller.member.mapper.IMemberCouponMapper;
import com.ruoyi.app.controller.shop.mapper.IShopConfigMapper;
import com.ruoyi.common.constant.CouponConstants;
import com.ruoyi.common.entity.DO.member.MemberCouponDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("AsyncActivityServiceImpl")
@Slf4j
public class ApiAsyncActivityServiceImpl implements IApiAsyncActivityService {

    @Resource
    private ICouponMapper couponMapper;

    @Resource
    private IMemberCouponMapper memberCouponMapper;

    @Override
    public void registerSendCoupon(Long memberId, Long shopId) {
        //TODO：判断用户是否已有注册赠送的优惠券
        int num = memberCouponMapper.countByRegisterActivity(memberId);
        if (num > 0) {
            return;
        }
        //TODO：查找优惠券
        List<Long> couponIdList = couponMapper.searchActivityCoupon(shopId);
        if(couponIdList.size()>0){
            couponIdList.forEach(x->{
                MemberCouponDO memberCouponDO = new MemberCouponDO();
                memberCouponDO.setCouponId(x);
                memberCouponDO.setMemberId(memberId);
                memberCouponDO.setMemberCouponStatus(CouponConstants.MemberCouponStatusEnum.NOT_USE.getValue());
                memberCouponDO.setActivityId(1L);
                memberCouponDO.setReceiveChannelDesc("来自：会员授权手机号成功赠送");
                memberCouponDO.setCreateTime(new Date());
                memberCouponMapper.insertSelective(memberCouponDO);
            });
        }
    }
}
